#include "JScriptHighlighter.h"

JScriptHighlighter::JScriptHighlighter(QTextDocument *parent)
	: QSyntaxHighlighter(parent)
{
	HighlightingRule rule;
	
	// Funciton highlighting : blue font
	functionFormat.setForeground(Qt::blue);
	//functionFormat.setFontWeight(QFont::Bold);
	rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
	rule.format = functionFormat;
	highlightingRules.append(rule);

	// Python keywords : orange font
	keywordFormat.setForeground(QColor(255,165,0));
	keywordFormat.setFontWeight(QFont::Bold);
	QStringList keywordPatterns;
	keywordPatterns << "\\band\\b" << "\\bdel\\b" << "\\bfrom\\b"
		<< "\\bnot\\b" << "\\bwhile\\b" << "\\bas\\b"
		<< "\\belif\\b" << "\\bglobal\\b" << "\\bor\\b"
		<< "\\bwith\\b" << "\\bassert\\b" << "\\belse\\b"
		<< "\\bif\\b" << "\\bpass\\b" << "\\byield\\b"
		<< "\\bbreak\\b" << "\\bexcept\\b" << "\\bimport\\b"
		<< "\\bprint\\b" << "\\bclass\\b" << "\\bexec\\b"
		<< "\\bin\\b" << "\\braise\\b" << "\\bcontinue\\b"
		<< "\\bfinally\\b" << "\\bis\\b" << "\\breturn\\b"
		<< "\\bdef\\b" << "\\bfor\\b" << "\\blambda\\b" << "\\btry\\b";
	foreach (const QString &pattern, keywordPatterns) {
		rule.pattern = QRegExp(pattern);
		rule.format = keywordFormat;
		highlightingRules.append(rule);
	}

	// Python standard objects : purple
	classFormat.setForeground(Qt::darkMagenta);
	classFormat.setFontWeight(QFont::Bold);
	QStringList classPatterns;
	classPatterns << "\\bJ[A-Za-z]+\\b" << "\\bint\\b"
		<< "\\bfloat\\b" << "\\blong\\b" << "\\bcomplex\\b"
		<< "\\bstr\\b" << "\\bunicode\\b" << "\\blist\\b"
		<< "\\btuple\\b" << "\\bbuffer\\b" << "\\bxrange\\b"
		<< "\\bset\\b" << "\\bfrozenset\\b" << "\\bdict\\b"
		<< "\\bfile\\b" << "\\bNone\\b" << "\\bEllipsis\\b"
		<< "\\bTrue\\b" << "\\bFalse\\b" << "\\bdir\\b";
	foreach (const QString &pattern2, classPatterns) {
		rule.pattern = QRegExp(pattern2);
		rule.format = classFormat;
		highlightingRules.append(rule);
	}

	// Python single line comment : red
	singleLineCommentFormat.setForeground(QColor(200,0,0));
	rule.pattern = QRegExp("#[^\n]*");
	rule.format = singleLineCommentFormat;
	highlightingRules.append(rule);

	// Python multi-line comment : green
	multiLineCommentFormat.setForeground(Qt::darkGreen);
	commentStartExpression = QRegExp("\"\"\"");
	commentEndExpression = QRegExp("\"\"\"");

	// Python string highlight : green
	quotationFormat.setForeground(Qt::darkGreen);
	rule.pattern = QRegExp("\".*\"");
	rule.format = quotationFormat;
	highlightingRules.append(rule);
	rule.pattern = QRegExp("'.*'");
	rule.format = quotationFormat;
	highlightingRules.append(rule);

}

JScriptHighlighter::~JScriptHighlighter()
{

}

void JScriptHighlighter::highlightBlock(const QString &text)
{
	foreach (const HighlightingRule &rule, highlightingRules) {
		QRegExp expression(rule.pattern);
		int index = expression.indexIn(text);
		while (index >= 0) {
			int length = expression.matchedLength();
			setFormat(index, length, rule.format);
			index = expression.indexIn(text, index + length);
		}
	}
	setCurrentBlockState(0);

	int startIndex = 0;
	if (previousBlockState() != 1)
		startIndex = commentStartExpression.indexIn(text);

	while (startIndex >= 0) {
		int endIndex = commentEndExpression.indexIn(text, startIndex);
		int commentLength;
		if (endIndex == -1) {
			setCurrentBlockState(1);
			commentLength = text.length() - startIndex;
		} else {
			commentLength = endIndex - startIndex
				+ commentEndExpression.matchedLength();
		}
		setFormat(startIndex, commentLength, multiLineCommentFormat);
		startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
	}
}